import request from '@/utils/request';
import {object2Url} from "@/utils/trans";

/**
 * 分页查询用户
 * @param params 查询条件
 */
export async function pageMerchant(params) {
  const res = await request.post('/business/MerchantManagement/pageList', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 二级代理查询查询用户
 * @param params 查询条件
 */
export async function secondaryAgentMerchantId(params) {
  const res = await request.post('/business/MerchantManagement/secondaryAgentMerchantId', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}


/*
/!**
 * 二级代理查询查询用户
 * @param params 查询条件
 *!/
export async function secondaryAgentMerchantId(params) {
  const res = await request.post('/business/MerchantManagement/secondaryAgentMerchantId', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
*/

/**
 * 分页查询用户
 * @param params 查询条件
 */
export async function pageMerchantList(params) {
  const res = await request.post('/business/MerchantManagement/pageList', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询商家活动
 * @param
 */
export async function merchantId(id) {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua,
    merchantId:id
  };
  let url = `/business/shopCode/code/merchantId${object2Url(us)}`

  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 商家冻结启用
 * @param data 商户信息
 */
export async function pointRecharges(id, status) {
  const res = await request.post('/business/MerchantManagement/freezeUpdate', {
    id,
    status
  });
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 查询用户列表
 * @param params 查询条件
 */
export async function listUsers(params) {
  const res = await request.post('/system/pageList', params);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询用户
 * @param id 用户id
 */
export async function getUser(id) {
  const res = await request.get('/merchantManagement/merchantAccount/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 查询商户列表
 * @param params 查询条件
 */
export async function listMerchant(params) {
  const res = await request.post('/business/MerchantManagement/listMerchant', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查看商家列表
 * @param params 查询条件
 */
export async function oemMerchantId(params) {
  const res = await request.post('/business/MerchantManagement/oemMerchantId', params);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}




/**
 * 查询抖音/快手配置
 * @param data 商户信息
 */
export async function dyKsPage(data) {
  const res = await request.post('/business/systemConfiguration/dyKsPage', data);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 增加抖音/快手配置
 * @param data 商户信息
 */
export async function updateDy(data) {
  const res = await request.post('/business/systemConfiguration/dyKsAdd', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
// 保存萤石云配置
export async function saveYsySet(data) {
  const res = await request.post('/business/ysyConfiguration/save', data);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

// 查询萤石云配置
export async function queryYsySet(data) {
  const res = await request.post('/business/ysyConfiguration/page', data);
  if (res.data.code === 2000) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加商户
 * @param data 商户信息
 */
export async function addUser(data) {
  const res = await request.post('/business/MerchantManagement/add', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改商户
 * @param data 商户信息
 */
export async function updateUser(data) {
  const res = await request.post('/business/MerchantManagement/update', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 重置密码
 * @param data 商户信息
 */
export async function merchantPassword(data) {
  const res = await request.post('/business/MerchantManagement/resetPasswords', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 商家充值
 * @param data 商户信息
 */
export async function pointRecharge(data) {
  const res = await request.post('/business/MerchantManagement/pointRecharge', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}


/**
 * 查询充值记录
 * @param
 */
export async function recordPageId(id) {
  const ts = new Date().getTime();
  const ua = navigator.userAgent;
  const us = {
    ts,
    ua,
    id:id,
    types:1,
  };
  let url = `/business/MerchantManagement/rechargeRecordPageId${object2Url(us)}`

  const res = await request.get(url, us);
  if (res.data.code === 2000) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除用户
 * @param id 用户id
 */
export async function removeUser(params) {
  const res = await request.post('/business/MerchantManagement/delete', params);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除用户
 * @param data 用户id集合
 */
export async function removeUsers(data) {
  const res = await request.post('/business/MerchantManagement/delete', data);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改用户状态
 * @param userId 用户id
 * @param status 状态
 */
export async function updateUserStatus(userId, status) {
  const res = await request.put('/system/user/status', {
    userId,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 重置用户密码
 * @param userId 用户id
 * @param password 密码
 * @returns {Promise<string>}
 */
export async function updateUserPassword(params) {
  const res = await request.post('/auth/user/updatePassword', params);
  if (res.data.code === 2000) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导入用户
 * @param file excel文件
 */
export async function importUsers(file) {
  const formData = new FormData();
  formData.append('file', file);
  const res = await request.post('/system/user/import', formData);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查用户是否存在
 * @param field 检查的字段
 * @param value 字段的值
 * @param id 修改时的id
 */
export async function checkExistence(params) {
  const res = await request.post('/auth/user/existence', params);
  if (res.data.code === 2000 && res.data.data != null) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
